package gui;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mariano Zapata
 */
public class PanelConfiguracionAplicaciones extends javax.swing.JPanel {

    private DefaultTableModel modelo;
    
    /** Creates new form PanelConfiguracionAplicaciones */
    public PanelConfiguracionAplicaciones(List<String []> pares)
    {
        initComponents();
        modelo= (DefaultTableModel) tabla.getModel();
        for (String [] par : pares)
            modelo.addRow(par);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        eliminar = new javax.swing.JButton();
        agregar = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(659, 300));

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Extensión", "Ruta de la aplicación"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabla.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tabla);

        eliminar.setText("Eliminar");
        eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminar(evt);
            }
        });

        agregar.setText("Agregar");
        agregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregar(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(agregar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(eliminar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(eliminar)
                    .addComponent(agregar))
                .addContainerGap(35, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void eliminar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminar
        int fila= tabla.getSelectedRow();
        if (fila >= 0)
            modelo.removeRow(fila);
    }//GEN-LAST:event_eliminar

    private void agregar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregar
        String extension= JOptionPane.showInputDialog(this, "Extensión:", "Agregar aplicación", JOptionPane.PLAIN_MESSAGE);
        if (extension != null)
        {
            if (this.validarExtension(extension))
            {
                Selector selector= new Selector(new FileFilter[0], "Agregar aplicación", true);
                File ejecutable= selector.hacerVisibleSelectorAbrir(this);
                if (ejecutable != null)
                {
                    String [] par= new String[2];
                    par[0]= extension;
                    par[1]= ejecutable.getAbsolutePath();
                    modelo.addRow(par);
                }
            }
            else
                JOptionPane.showMessageDialog(this, "Debe ingresar una extensión que comience con el caracter \".\"",
                        "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_agregar

    public List<String []> getAplicaciones()
    {
        ArrayList<String []> ret= new ArrayList<String []>();
        int filas= modelo.getRowCount();
        for (int k= 0; k < filas; k++)
        {
            String [] parActual= new String[2];
            parActual[0]= (String) modelo.getValueAt(k, 0);
            parActual[1]= (String) modelo.getValueAt(k, 1);
            ret.add(parActual);
        }
        return ret;
    }
    
    private boolean validarExtension(String ext)
    {
        if (ext.isEmpty())
            return false;
        if (!ext.startsWith("."))
            return false;
        return true;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton agregar;
    private javax.swing.JButton eliminar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabla;
    // End of variables declaration//GEN-END:variables
}
